package game;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import player.Player;
import util.Dictionnaire;

public class LetterBag {

	public static HashMap<String, Integer> ValLetter = new HashMap<String,Integer>(26);
	public static HashMap<String, Integer> OccurLetter =new HashMap<String,Integer>(26);

	private Map<String, Integer> LetterBag =OccurLetter;
	public static String[] Alph = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

	
	
	/**
	 * Constructeur par default(dico en FR)
	 */
	LetterBag()
	{
		
		
		init_LetterBag_Manual();
	}
	
	/**constructeur suivant un dictionnaire determiner
	 * @param dico
	 */
	LetterBag(Dictionnaire dico)
	{
		ValLetter=dico.getValeur();
		OccurLetter=dico.getOccur();
		LetterBag=OccurLetter;
	}
	
	
	/**
	 * Initialise les variables de valeur et d'occurence des lettre manuelement en FR
	 */
	public void init_LetterBag_Manual()
	{
	

		ValLetter.put("a",1);
		ValLetter.put("b",3);
		ValLetter.put("c",3);
		ValLetter.put("d",2);
		ValLetter.put("e",1);
		ValLetter.put("f",4);
		ValLetter.put("g",2);
		ValLetter.put("h",4);
		ValLetter.put("i",1);
		ValLetter.put("j",8);
		ValLetter.put("k",10);
		ValLetter.put("l",1);
		ValLetter.put("m",2);
		ValLetter.put("n",1);
		ValLetter.put("o",1);
		ValLetter.put("p",3);
		ValLetter.put("q",8);
		ValLetter.put("r",1);
		ValLetter.put("s",1);
		ValLetter.put("t",1);
		ValLetter.put("u",1);
		ValLetter.put("v",4);
		ValLetter.put("w",10);
		ValLetter.put("x",10);
		ValLetter.put("y",10);
		ValLetter.put("z",10);

		OccurLetter.put("a",9);
		OccurLetter.put("b",2);
		OccurLetter.put("c",2);
		OccurLetter.put("d",3);
		OccurLetter.put("e",15);
		OccurLetter.put("f",2);
		OccurLetter.put("g",2);
		OccurLetter.put("h",2);
		OccurLetter.put("i",8);
		OccurLetter.put("j",1);
		OccurLetter.put("k",1);
		OccurLetter.put("l",5);
		OccurLetter.put("m",4);
		OccurLetter.put("n",6);
		OccurLetter.put("o",6);
		OccurLetter.put("p",2);
		OccurLetter.put("q",1);
		OccurLetter.put("r",6);
		OccurLetter.put("s",6);
		OccurLetter.put("t",6);
		OccurLetter.put("u",6);
		OccurLetter.put("v",2);
		OccurLetter.put("w",1);
		OccurLetter.put("x",1);
		OccurLetter.put("y",1);
		OccurLetter.put("z",1);

	}


	
	/**
	 * Tant que le joueur en parametre(qui pioche) n'a pas 7 lettre on lui en donne aleatoirement en piochant dans le sac de lettre(LetterBag)
	 * @param p
	 */
	public void draw_Letter(Player p)
	{
		Random r = new Random();
		while(p.getLetters().size()!=7)
		{
			String s = Alph[r.nextInt(26)];
			//      System.out.println("Lettre Alea :"+s);
			int occur=getLetterBag().get(s);
			if(occur>0)
			{
				getLetterBag().put(s,occur-1);
				p.addLetters(new Jeton(s));
			}
			if(occur==0)
			{
				getLetterBag().remove(s);
			}
		}
		
	}



	public  Map<String, Integer> getLetterBag() {
		return LetterBag;
	}



	public void setLetterBag(Map<String, Integer> letterBag) {
		LetterBag = letterBag;
	}

	
}
